package com.jiuzhe.commercial.ui.house.editor;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.jiangyy.easydialog.SingleChoiceDialog;
import com.jiuzhe.commercial.R;
import com.jiuzhe.commercial.base.BaseActivity;
import com.jiuzhe.commercial.bean.HouseStayListBean;
import com.jiuzhe.commercial.constant.Constants;
import com.jiuzhe.commercial.http.CallBack;
import com.jiuzhe.commercial.http.HttpClient;
import com.jiuzhe.commercial.utils.RxBus;
import com.jiuzhe.commercial.utils.SPUtil;
import com.jiuzhe.commercial.utils.UIUtils;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by huanghao on 2018/6/7.
 */

public  class EditorHomeAssortActivity extends BaseActivity{
    @BindView(R.id.et_city)
    TextView etCity;
    @BindView(R.id.tv_wifi)
    TextView tvWifi;
    @BindView(R.id.tv_room)
    TextView tvRoom;
    @BindView(R.id.tv_bed)
    TextView tvBed;
    @BindView(R.id.tv_bathroom)
    TextView tvBathroom;
    private int type;
    private int roomType;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_editor_homestay_type);
        setTitle("房源配套设施");
        setRringTitle("保存");
        setOnClik();
    }

    private void setOnClik() {
        tv_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addHomeStayType();
            }
        });
    }



    @Override
    protected void loadData() {
        if (null != Constants.HOUSESTAYLISTBEAN) {
            tvBathroom.setText(Constants.HOUSESTAYLISTBEAN.getToiletNum() + "" + "个");
            tvBed.setText(Constants.HOUSESTAYLISTBEAN.getBedNum() + "" + "张");
            tvRoom.setText(Constants.HOUSESTAYLISTBEAN.getBedroomNum() + "" + "间");
            String type = Constants.HOUSESTAYLISTBEAN.getRoomType();

        if (("0").equals(type)) {
            etCity.setText("公寓型");
        } else if (("1").equals(type)) {
            etCity.setText("酒店型");
        } else if (("2").equals(type)) {
            etCity.setText("住宅型");
        }

    }

          if (Constants.HOUSESTAYLISTBEAN.getWifi() == 1) {
              tvWifi.setText("有");
          }else {
              tvWifi.setText("无");
          }
    }


    @OnClick({R.id.et_city,R.id.tv_wifi,R.id.tv_room,R.id.tv_bed,R.id.tv_bathroom})
    public void onclick(View view) {
        switch (view.getId()) {
            case R.id.tv_wifi:
                new SingleChoiceDialog.Builder(this).setTitle("提示")
                        .addList(new String[]{"有", "无"})
                        .setOnItemClickListener(new SingleChoiceDialog.OnItemClickListener() {
                            @Override
                            public void OnItemClick(String title, int position) {
                                if ("有".equals(title)){
                                  //  houseStayBean.setWifi("1");
                                }else {
                                   // houseStayBean.setWifi("0");
                                }
                                tvWifi.setText(title);
                            }
                        }).show();
                break;
            case R.id.tv_room:
                type = 1;
                getType(type);
                break;
            case R.id.tv_bed:
                type = 2;
                getType(type);
                break;
            case R.id.tv_bathroom:
                type = 3;
                getType(type);
                break;
            case R.id.et_city:
                new SingleChoiceDialog.Builder(this).setTitle("提示")
                        .addList(new String[]{"公寓型", "酒店型","住宅型"})
                        .setOnItemClickListener(new SingleChoiceDialog.OnItemClickListener() {
                            @Override
                            public void OnItemClick(String title, int position) {
                                etCity.setText(title);
                                setRoomType(title);
                            }
                        }).show();
                break;
        }
    }

    private int setRoomType(String title) {
        if ("公寓型".equals(title)){
            roomType = 0;
        }else if ("酒店型".equals(title)){
            roomType = 1;
        }else if ("住宅型".equals(title)){
            roomType = 2;
        }
        return  roomType;
    }

    private void getType(final int type) {
        new SingleChoiceDialog.Builder(this).setTitle("提示")
                .addList(new String[]{"1", "2","3","4"})
                .setOnItemClickListener(new SingleChoiceDialog.OnItemClickListener() {
                    @Override
                    public void OnItemClick(String title, int position) {
                        if (type == 1){
                            tvRoom.setText(title + "间");
                          //  houseStayBean.setBedroomNum(Integer.parseInt(title));
                        }else if (type == 2){
                            tvBed.setText(title + "张");
                           // houseStayBean.setBedNum(Integer.parseInt(title));
                        }else if (type == 3){
                            tvBathroom.setText(title + "个");}
                        //houseStayBean.setToiletNum(Integer.parseInt(title));
                    }
                }).show();
    }

    private void addHomeStayType() {
        final HouseStayListBean houseStayListBean = new HouseStayListBean();
        String tType = etCity.getText().toString().trim();
        String tWift = tvWifi.getText().toString().trim();
        String tRoom = tvRoom.getText().toString().trim();
        String tBed = tvBed.getText().toString().trim();
        String tBathroom = tvBathroom.getText().toString().trim();
        if (TextUtils.isEmpty(tType)) {
            UIUtils.showToastSafe("请填写房源类型信息！");
            return;
        }
        if (TextUtils.isEmpty(tWift)) {
            UIUtils.showToastSafe("请选择WiFi网格信息！");
            return;
        }
        if (TextUtils.isEmpty(tRoom)) {
            UIUtils.showToastSafe("请选择卧室间数！");
            return;
        }
        if (TextUtils.isEmpty(tBed)) {
            UIUtils.showToastSafe("请选择床位数！");
            return;
        }
        if (TextUtils.isEmpty(tBathroom)) {
            UIUtils.showToastSafe("请选择卫生间数！");
            return;
        }


        HashMap<String, String> map = new HashMap<>();
        String token = SPUtil.getString(EditorHomeAssortActivity.this, Constants.TOKEN);
        String id = SPUtil.getString(EditorHomeAssortActivity.this, Constants.ID);
        String autoSignStr = Constants.SKUUPDATING.substring(Constants.SKUUPDATING.lastIndexOf("0/") + 1) + "?token=";
        String signStr = UIUtils.Md5(autoSignStr + token);
        map.put("token", id);
        map.put("sign", signStr);
        map.put("skuId", Constants.HOUSESTAYLISTBEAN.getId());
        if (tWift.equals("有")){
            map.put("wifi", "1");
        }else {
            map.put("wifi", "0");
        }
        map.put("roomType", setRoomType(tType) + "");
        map.put("bedroomNum", tRoom.substring(0,1));
        map.put("toiletNum", tBathroom.substring(0,1));
        map.put("bedNum",tBed.substring(0,1));
        HttpClient.post(this, Constants.SKUUPDATING, map, new CallBack<String>() {
            @Override
            public void onSuccess(String result) {
               UIUtils.showToastSafe("修改成功");
                RxBus.getInstance().post(houseStayListBean);
               finish();
            }
        });
    }
}
